package mapkit.carto
{

	public class Envelope
	{
		private var _srid:String="EPSG:4326";

		final public function get srid():String
		{
			return _srid;
		}

		final public function set srid(value:String):void
		{
			_srid=value;
		}

		public var minX:Number=-180;

		public var minY:Number=-90;

		public var maxX:Number=180;

		public var maxY:Number=90;

		final public function get center():Location
		{
			return new Location(0.5 * (minX + maxX), 0.5 * (minY + maxY), srid);
		}

		final public function set center(value:Location):void
		{
			var w:Number=width;
			var h:Number=height;
			minX=value.x - 0.5 * w;
			minY=value.y - 0.5 * h;
			maxX=value.x + 0.5 * w;
			maxY=value.y + 0.5 * h;
		}

		final public function get width():Number
		{
			return maxX - minX;
		}

		final public function get height():Number
		{
			return maxY - minY;
		}

		public function Envelope(minx_:Number=-180, miny_:Number=-90, maxx_:Number=180,
			maxy_:Number=90, srid_:String="EPSG:4326")
		{
			minX=minx_;
			minY=miny_;
			maxX=maxx_;
			maxY=maxy_;
			srid=srid_;
		}

		final public function pan(dx_:Number, dy_:Number):void
		{
			var c:Location=center;
			c.x-=dx_;
			c.y-=dy_;
			center=c;
		}

		final public function zoom(ratio_:Number, center_:Location=null):void
		{
			var c:Location=center;
			var w:Number=width;
			var h:Number=height;
			minX=c.x - 0.5 * ratio_ * w;
			minY=c.y - 0.5 * ratio_ * h;
			maxX=c.x + 0.5 * ratio_ * w;
			maxY=c.y + 0.5 * ratio_ * h;
			if (center_ !== null)
			{
				center=center_;
			}
		}

		final public function encloseMapPoint(pt_:Location):void
		{
			minX=Math.min(pt_.x, minX);
			maxX=Math.max(pt_.x, maxX);
			minY=Math.min(pt_.y, minY);
			maxY=Math.max(pt_.y, maxY);
		}

		final public function encloseExtent(extent_:Envelope):void
		{
			minX=Math.min(extent_.minX, minX);
			maxX=Math.max(extent_.maxX, maxX);
			minY=Math.min(extent_.minY, minY);
			maxY=Math.max(extent_.maxY, maxY);
		}

		final public function enclose(... args):void
		{
			for each (var arg:*in args)
			{
				if (arg is Location)
				{
					encloseMapPoint(arg);
				}
				else if (arg is Envelope)
				{
					encloseExtent(arg);
				}
			}
		}

	}
}
